package cn.hx.mall.permissions.controller;

import cn.hx.mall.permissions.entity.page.Page;
import cn.hx.mall.permissions.entity.Menu;
import cn.hx.mall.permissions.service.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 后台菜单表(Menu)表控制层
 *
 * @author makejava
 * @since 2022-11-02 17:02:35
 */
@Controller
@RequestMapping("permissions")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

//    查询一级菜单
    @GetMapping("menu")
    public ModelAndView getMenu(Integer id, Page page) {
        Menu menu1 = menuService.queryById(id);
        ModelAndView modelAndView = new ModelAndView("/permissions/menu");

        Menu menu = new Menu();

//        一级菜单
        if (menu1==null){
            //        一级菜单
            menu.setParentId(0);
            menu.setLevel(0);
            List<Menu> menuList = menuService.queryAllByLimit(menu, page.getPage(), page.getLimit());
            page.setSearch(menu.getParentId()+","+menu.getLevel());
            Page page1 = menuService.get(page);
            modelAndView.addObject("id", id);
            modelAndView.addObject("menuList", menuList);
            modelAndView.addObject("pageInfo",page1);
        }else {
//            二级菜单
            menu.setParentId(menu1.getId());
            menu.setLevel(1);
            List<Menu> menuList = menuService.queryAllByLimit(menu, page.getPage(), page.getLimit());
            page.setSearch(menu.getParentId()+","+menu.getLevel());
            Page page1 = menuService.get(page);
            modelAndView.addObject("id", id);
            modelAndView.addObject("menuList", menuList);
            modelAndView.addObject("pageInfo",page1);
        }

        return modelAndView;
//    }
       /* page.setSearch(id.toString());
//        二级菜单
        menu.setId(id);
        menu.setLevel(1);
        List<Menu> menuList = menuService.queryAllByLimit(menu,page.getPage(), page.getLimit());
        Page page1 =menuService.get(page);
        modelAndView.addObject("menuList",menuList);
        modelAndView.addObject("pageInfo2",page1);
        return modelAndView;*/
    }
//    添加用户信息
    @GetMapping("addMenu")
    public ModelAndView getAddMenu(){
        ModelAndView modelAndView = new ModelAndView("/permissions/addMenu");
        List<Menu> menuTitleList = menuService.selectAllMenu();
        modelAndView.addObject("menuTitleList",menuTitleList);
        return modelAndView;
    }
//    提交添加用户信息
    @PostMapping("addMenu")
    public ModelAndView postAddMenu(Menu menu){
        ModelAndView modelAndView = new ModelAndView("redirect:/permissions/menu");
        if (menu.getId()!=0){
            menu.setLevel(1);
        }else {
            menu.setLevel(0);
        }
        menu.setParentId(menu.getId());
        menu.setId(null);
        menu.setCreateTime(new Date());
        menuService.insert(menu);
        return modelAndView;
    }
//    删除菜单
    @GetMapping("deleteMenu/{id}")
    public String getDeleteMenu(@PathVariable Integer id){
        menuService.deleteById(id);
        return "redirect:/permissions/menu";
    }
//    修改菜单信息回显
    @GetMapping("updateMenu/{id}")
    public ModelAndView getUpdateMenu(@PathVariable Integer id){
        ModelAndView modelAndView = new ModelAndView("/permissions/updateMenu");
        modelAndView.addObject("menu",menuService.queryById(id));
        modelAndView.addObject("menuTitleList",menuService.selectAllMenu());
        return modelAndView;
    }
//    确定修改
    @PostMapping("updateMenu/{id}")
    public String postUpdateMenu(Menu menu , @PathVariable Integer id){
        menu.setId(id);
        menuService.update(menu);
        return "redirect:/permissions/menu";
    }
//    修改状态
    @GetMapping("updateHidden/{id}/{hidden}")
    public ModelAndView getHidden(@PathVariable Integer id , @PathVariable Integer hidden){
        ModelAndView modelAndView = new ModelAndView("redirect:/permissions/menu");
        Menu menu = new Menu();
        menu.setId(id);
        menu.setHidden(hidden);
        menuService.update(menu);
        return modelAndView;
    }

//    查看下级
    @GetMapping("selectAllJunior/{id}")
    public ModelAndView getSelectAllJunior(@PathVariable Integer id){
        ModelAndView modelAndView = new ModelAndView("/permissions/menu");
        List<Menu> menuList = menuService.selectAllSecondLevel(id);
        modelAndView.addObject("menuList",menuList);
        return modelAndView;
    }

}